/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ita.escolaita.view.converter;

import br.ita.escolaita.model.entity.Professor;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.model.SelectItem;

/**
 *
 * @author Rafael
 */
public class ProfessorConverter implements Converter {

    //@SuppressWarnings({"unchecked", "deprecation"})
    public Object getAsObject(FacesContext context, UIComponent comp, String value) throws ConverterException {
        UISelectItems selectItems = (UISelectItems) comp.getChildren().get(1);
        List listaSelectItem = (List) selectItems.getValue();
        
        for (Object o: listaSelectItem) {
            SelectItem  item = (SelectItem) o;
            Professor p = (Professor) item.getValue();
            if (p.getId().equals(Integer.valueOf(value))){
                return p;
            }
        }

        return null;
    }

    public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException {
        if (object == null) {
            return null;
        }
        if (object instanceof String){
            return "";
        }
        Professor p = (Professor) object;
        return p.getId().toString();
    }
}
